﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HangzhouChipProject
{
    class MotorsControl
    {

        static bool[] en = new bool[8];
        static uint clk;
      
        public static short MotionOpen(short channel, short param)
        {
            return mc.GTN_Open(channel, param);
        }
        public static short MotionReset(short CORE)
        {
            return mc.GTN_Reset(CORE);
        }
        //设置平滑停止和紧急停止Io
        public static short MotionSetStopio(short CORE, short axis, short stopTepy, short inputType, short inputindex)
        {
            return mc.GTN_SetStopIo(CORE, axis, stopTepy, inputType, inputindex);
        }
        public static short MotorsInit(short CORE)
        {
            mc.GTN_LoadConfig(CORE, "gtn_core1.cfg");
            return mc.GTN_ClrSts(CORE, 1, 8);
        }
        public static short MotorsPoseClear(short CORE, short Axis, short count)
        {

            return mc.GTN_ZeroPos(CORE, Axis, count);
        }
        public static bool MotorsAxisOnoff(short CORE, short axis)
        {

            if (!en[axis - 1])
            {
                mc.GTN_AxisOn(CORE, axis); //上伺服

            }
            else
            {
                mc.GTN_AxisOff(CORE, axis); //下伺服

            }
            return en[axis - 1] = !en[axis - 1];
        }
        //正向运动
        public static void JogPositiveMove(short CORE, short axis, mc.TJogPrm jog, double vel)
        {
            mc.GTN_PrfJog(CORE, axis); // 设置为Jog运动模式
            mc.GTN_SetJogPrm(CORE, axis, ref jog); // 设置Jog运动参数
            mc.GTN_SetVel(CORE, axis, vel);  // 设置目标速度
            mc.GTN_Update(CORE, 1 << (axis - 1));    // 更新轴运动
            mc.GTN_ClrSts(CORE, axis, 1);
        }
        //负向运动
        public static void JogNegativeMove(short CORE, short axis, mc.TJogPrm jog, double vel)
        {
            mc.GTN_PrfJog(CORE, axis); // 设置为Jog运动模式
            mc.GTN_SetJogPrm(CORE, axis, ref jog); // 设置Jog运动参数
            mc.GTN_SetVel(CORE, axis, -vel);  // 设置目标速度
            mc.GTN_Update(CORE, 1 << (axis - 1));    // 更新轴运动
            mc.GTN_ClrSts(CORE, axis, 1);
        }
        public static short MotorStop(short CORE, short axis)
        {
            return mc.GTN_Stop(CORE, 1 << (axis - 1), 0);   // 平滑停止
        }
        /// <summary>
        /// 单轴移动
        /// </summary>
        /// <param name="CORE"></param>
        /// <param name="axis"></param>
        /// <param name="trap"></param>
        /// <param name="vel"></param>
        /// <param name="pose"></param>
        /// <returns></returns>

        public static int PointMove(short CORE, short axis, mc.TTrapPrm trapPrm, double vel, int pose)
        {

            DateTime dateBegin = DateTime.Now;
            DateTime dateEnd;
            double timeSpan;
            int state = -1;
            short Srtn = mc.GTN_SetAxisBand(CORE, axis, 20, 5);
            mc.GTN_PrfTrap(CORE, axis); // 设置为点位运动模式
            mc.GTN_SetTrapPrm(CORE, axis, ref trapPrm); // 设置点位运动参数
            mc.GTN_SetVel(CORE, axis, vel);  // 设置目标速度
            mc.GTN_SetPos(CORE, axis, pose);  // 设置目标位置
            mc.GTN_Update(CORE, 1 << (axis - 1));    // 更新轴运动
            while ((state & 0x400) == 0x400)
            {
                mc.GTN_GetSts(CORE, axis, out state, 1, out uint pc);
                dateEnd = DateTime.Now;
                timeSpan = ExecDateDiff(dateBegin, dateEnd);
                if (timeSpan > 40000)
                {
                    MessageBox.Show("第" + axis + "轴运动超时", "点位运动", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    break;
                }
            };
            return state;




        }
        /// <summary>
        /// 程序执行时间测试
        /// </summary>
        /// <param name="dateBegin">开始时间</param>
        /// <param name="dateEnd">结束时间</param>
        /// <returns>返回(秒)单位，比如: 0.00239秒</returns>
        public static double ExecDateDiff(DateTime dateBegin, DateTime dateEnd)
        {
            TimeSpan ts1 = new TimeSpan(dateBegin.Ticks);
            TimeSpan ts2 = new TimeSpan(dateEnd.Ticks);
            TimeSpan ts3 = ts1.Subtract(ts2).Duration();
            //你想转的格式
            return ts3.TotalMilliseconds;
        }
        internal static void MonitorReadPose(short cORE, int i, out double prfvel, out double prfpos, out double encvel, out double encpos)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 飞拍运动 
        /// </summary>
        /// <param name="CORE"> 核号</param>
        /// <param name="axis">轴号</param>
        /// <param name="cycleindex">拍几行</param>
        /// <param name="trapPrm"></param>
        /// <param name="vel">速度</param>
        /// <param name="pose">第一个X轴移动距离，第二个是Y轴移动步长</param>
        /// <returns></returns>
        public static void Flymove(short CORE, short[] axis, int cycleindex, mc.TTrapPrm trapPrm, double[] vel, int[] pose, int interval)
        {
            setCompareMode(new short[] { 3,4 }, new short[] { 1,1 }, 2, 1, 1, 5, 5, 0, 100);
            setCompareData_Pso(interval); // 等差模式
            for (int i = 0; i < cycleindex; i++)
            {
                startCompare();
                if (i % 2 == 0)
                {
                    PointMove(CORE, axis[0], trapPrm, vel[0], pose[2]);
                }
                else
                {
                    PointMove(CORE, axis[0], trapPrm, vel[0], pose[0]);
                }
                stopCompare();
                if (i == cycleindex-1) break;
                int n = pose[1] + (i + 1) * pose[3];
                PointMove(CORE, axis[1], trapPrm, vel[1], n);
            }
            PointMove(CORE, axis[0], trapPrm, vel[0], pose[0]);
            PointMove(CORE, axis[1], trapPrm, vel[1], pose[1]);
        }

        /// <summary>
        /// 飞拍运动 
        /// </summary>
        /// <param name="CORE"> 核号</param>
        /// <param name="axis">轴号</param>
        /// <param name="cycleindex">拍几行</param>
        /// <param name="trapPrm"></param>
        /// <param name="vel">速度</param>
        /// <param name="pose">第一个X轴移，第二个是Y轴, 第三个是位移</param>
        /// <returns></returns>
        public static void Flymove2(short CORE, short[] axis, int row,int column, mc.TTrapPrm trapPrm, double[] vel, int[] pose)
        {
            bool isSucc = setCompareMode(new short[] { 3 }, new short[] { 1}, 2, 1, 1, 5, 5, 0, 100);
            if (!isSucc)
            {
                MessageBox.Show("setCompareMode failed");
            }
            isSucc = setCompareData_Pso(100); // 等差模式
            if (!isSucc)
            {
                MessageBox.Show("setCompareData_Pso failed");
            }
            int x = pose[0], y = pose[1];
            for (int r = 0; r < row - 1; r++)
            {
                if (r % 2 == 0)
                {
                    for (int c = 0; c < column; c++)
                    {
                        startCompare();
                        x += pose[2];
                        PointMove(CORE, axis[0], trapPrm, vel[0], x);
                        stopCompare();
                        // 拍照
                    }  
                }
                else
                {
                    for (int c = 0; c < column; c++)
                    {
                        startCompare();
                        x -= pose[2];
                        PointMove(CORE, axis[0], trapPrm, vel[0], x);
                        stopCompare();
                        // 拍照
                    }
                }
                y = y + pose[2];
                PointMove(CORE, axis[1], trapPrm, vel[1], y);

            }
            Parallel.Invoke(() =>
            {
                PointMove(CORE, axis[0], trapPrm, vel[0], pose[0]);
            },
            () =>
            {
                PointMove(CORE, axis[1], trapPrm, vel[1], pose[1]);
            });
        }

        /// <summary>
        /// s拾取产品，先移动三轴到物料位置，然后四轴抓取
        /// </summary>
        /// <param name="CORE"></param>
        /// <param name="axis"></param>
        /// <param name="trapPrm"></param>
        /// <param name="vel"></param>
        /// <param name="pose"></param>
        /// <returns></returns>
        public static void PickupProduct(short CORE, short[] axis, mc.TTrapPrm trapPrm, double[] vel, int[] pose)
        {
            Parallel.Invoke(() =>
            {
                // MotorsControl.ioSignalEXOMonitoring(CORE, 12, 9, 0);
                PointMove(CORE, axis[0], trapPrm, vel[0], pose[0]);
            }, () =>
            {
                PointMove(CORE, axis[1], trapPrm, vel[1], pose[1]);
            });
            PointMove(CORE, axis[2], trapPrm, vel[2], pose[2]);
            PointMove(CORE, axis[2], trapPrm, -vel[2], pose[2]);
        }
        /// <summary>
        /// 放下废料物品，先移动四轴再动三轴
        /// </summary>
        /// <param name="CORE"></param>
        /// <param name="axis"></param>
        /// <param name="trapPrm"></param>
        /// <param name="vel"></param>
        /// <param name="pose"></param>
        /// <returns></returns>
        public static int LayDownProduct(short CORE, short[] axis, mc.TTrapPrm trapPrm, double[] vel, int[] pose)
        {
            int S3;
            int S4;
            do
            {
                mc.GTN_GetSts(CORE, axis[0], out S3, 1, out uint pc);
            } while ((S3 & 0x400) == 0x400);
            //  MotorsControl.ioSignalEXOMonitoring(CORE, 12, 8, 0);
            PointMove(CORE, axis[1], trapPrm, vel[1], pose[1]);
            do
            {
                mc.GTN_GetSts(CORE, axis[1], out S4, 1, out uint pc);
            } while ((S4 & 0x400) == 0x400);
            PointMove(CORE, axis[0], trapPrm, vel[0], pose[0]);
       
            return S3;
        }
        /// <summary>
        /// DI信号监控,IO类型 "0"=正限位，"1"=负限位，"2"=驱动报警，"3"=原点开关，
        /// "4"=通用输入，"5"=电机到位信号
        /// </summary>
        /// <param name="CORE">轴卡</param>
        /// <param name="diTepy"></param>
        /// <returns></returns>
        public static int ioSignalEXIMonitoring(short CORE, short diTepy)
        {
            int PdiValue = -1;
            switch (diTepy)
            {
                case 0:
                    mc.GTN_GetDi(CORE, diTepy, out PdiValue);
                    break;
                case 1:
                    mc.GTN_GetDi(CORE, diTepy, out PdiValue);
                    break;
                case 2:
                    mc.GTN_GetDi(CORE, diTepy, out PdiValue);
                    break;
                case 3:
                    mc.GTN_GetDi(CORE, diTepy, out PdiValue);
                    break;
                case 4:
                    mc.GTN_GetDi(CORE, diTepy, out PdiValue);
                    break;
                case 5:
                    mc.GTN_GetDi(CORE, diTepy, out PdiValue);
                    break;
            }
            return PdiValue;
        }
        //
        /// <summary>
        /// 信号输出
        /// </summary>
        /// <param name="CORE"></param>
        /// <param name="diTepy">"10"=驱动器使能，"11"=报警清除，"12"=通用输出</param>
        /// <param name="PdoValue">"1"高电平，"0"低电平</param>
        /// <returns></returns>
        public static int ioSignalEXOMonitoring(short CORE, short diTepy, short Index, short PdoValue)
        {
            int setStart = -1;
            switch (diTepy)
            {
                case 10:
                    setStart = mc.GTN_SetDo(CORE, 10, PdoValue);
                    break;
                case 11:
                    setStart = mc.GTN_SetDo(CORE, 11, PdoValue);
                    break;
                case 12:
                    setStart = mc.GTN_SetDoBit(CORE, 12, Index, PdoValue);
                    break;
            }
            return setStart;
        }

        public static void MonitorReadPose(short CORE, short axis, out double prfpos, out double prfvel, out double encpos, out double encvel)
        {
            mc.GTN_GetPrfPos(CORE, axis, out prfpos, 1, out clk);
            mc.GTN_GetPrfVel(CORE, axis, out prfvel, 1, out clk);
            mc.GTN_GetEncPos(CORE, axis, out encpos, 1, out clk);
            mc.GTN_GetEncVel(CORE, axis, out encvel, 1, out clk);
        }

        /// <summary>
        /// 轴的运行状态
        /// CORE 卡号
        /// AXIS 运行轴
        /// count 数量
        /// </summary>
        /// <param name="CORE"></param>
        /// <param name="AXIS"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static string AxisStatus(short CORE, short AXIS, short count)
        {
            int stn = mc.GTN_GetSts(CORE, AXIS, out int iAxisSts, count, out uint uiClock);

            switch (stn)
            {
                case -1:
                    return "主机和运动控制器通讯失败";

                case -6:
                    return "打开运动控制器失败";
            }
            // 驱动器报警标志
            if ((iAxisSts & 0x1) != 0)
            {
                return "驱动器报警";
            }
            // 伺服报警标志
            if ((iAxisSts & 0x2) != 0)
            {
                return "伺服报警";
            }
            // 跟随误差越限标志
            if ((iAxisSts & 0x10) != 0)
            {
                return "运动出错";
            }
            // 正向限位
            if ((iAxisSts & 0x20) != 0)
            {
                return "正限位触发";
            }
            // 负向限位
            if ((iAxisSts & 0x40) != 0)
            {
                return "负限位触发";
            }
            // 平滑停止
            if ((iAxisSts & 0x80) != 0)
            {
                return "平滑停止触发";
            }

            // 急停标志
            if ((iAxisSts & 0x100) != 0)
            {
                return "急停触发";
            }
            // 伺服使能标志
            if ((iAxisSts & 0x200) != 0)
            {
                return "伺服使能";
            }
            // 规划器正在运动标志
            if ((iAxisSts & 0x400) != 0)
            {
                return "规划器正在运动";
            }
            if (iAxisSts == 0)
            {
                return "初始化成功";
            }
            else
            {
                return "未知指令";
            }


        }
        public static short MonitorGoHome(short CORE, short _cardNum, mc.THomePrm homePrm, mc.THomeStatus homeStatus)
        {
            mc.GTN_GetSts(CORE, _cardNum, out int pSta, 1, out uint pC);
            if ((pSta & 0x40) == 0x40)
            {
                mc.GTN_GetPos(CORE, 1, out int pos);
                mc.GTN_ClrSts(CORE, _cardNum, 1);
                mc.GTN_PrfTrap(CORE, _cardNum); // 设置为点位运动模式
                mc.GTN_GetTrapPrm(CORE, _cardNum, out mc.TTrapPrm trap);
                trap.acc = homePrm.acc;
                trap.dec = homePrm.dec;
                trap.smoothTime = homePrm.smoothTime;
                mc.GTN_SetTrapPrm(CORE, _cardNum, ref trap); // 设置点位运动参数
                mc.GTN_SetVel(CORE, _cardNum, 10);  // 设置目标速度
                mc.GTN_SetPos(CORE, _cardNum, pos + 10000);  // 设置目标位置
                mc.GTN_Update(CORE, 1 << (_cardNum - 1));    // 更新轴运动              
                do
                {
                    mc.GTN_GetSts(CORE, _cardNum, out pSta, 1, out pC);
                } while ((pSta & 0x400) == 0x400);

            }
            mc.THomePrm prm;
            //  mc.GTN_AxisOff(CORE, _cardNum);
            //打开使能
            //  mc.GTN_AxisOn(CORE, _cardNum);
            //设置回原点参数
            //  GTS.THomePrm prm = new GTS.THomePrm();
            //获取home参数
            mc.GTN_GetHomePrm(_cardNum, _cardNum, out prm);
            //回原点模式
            prm.mode = homePrm.mode;
            //搜索原点时的运动方向，1正向，0负方向
            prm.moveDir = homePrm.moveDir;
            //搜索index的运动方向，1正向，0负方向
            //prm.indexDir = 1;
            // 设置捕获沿：0-下降沿，1-上升沿 
            // prm.edge = 0;
            //用于设置触发器：取值-1 和[1,8]，i
            //-1 表示 使用的触发器和轴号对应，其它值表示使 用其它轴的触发器，
            //触发器用于实现高速 硬件捕获，默认设置为-1 即可 
            prm.triggerIndex = homePrm.triggerIndex;
            //回原点运动的搜索速度
            prm.velHigh = homePrm.velHigh;
            //回原点运动的定位速度
            prm.velLow = homePrm.velLow;
            //回原点运动的加速度
            prm.acc = homePrm.acc;
            // 回原点运动的减速
            prm.dec = homePrm.dec;
            // 回原点运动的平滑时间：取值[0,50]，单位：ms，
            //具体含义与 GTS 系列控制器点位运动相似
            prm.smoothTime = homePrm.smoothTime;
            // 设定的搜索 Home 的搜索范围，0 表示 搜索距离为 805306368 
            prm.searchHomeDistance = homePrm.searchHomeDistance;
            // 设定的搜索 Index 的搜索范围， 0 表示 搜索距离为 805306368 
            prm.searchIndexDistance = homePrm.searchIndexDistance;
            // 采用“限位回原点” 方式时，反方向离开 限位的脱离步长 
            prm.homeOffset = homePrm.homeOffset;
            // prm.escapeStep = 1000;
            //没有限位开关则取消限位,取消限位信息
            //GTS.GT_LmtsOn(_cardNum, i, 0);
            //清除轴状态
            mc.GTN_ClrSts(CORE, _cardNum, 1);
            //
            mc.GTN_ZeroPos(CORE, _cardNum, 1);
            //启动自动回原点
            short stn = mc.GTN_GoHome(CORE, _cardNum, ref prm);
            do
            {
                mc.GTN_GetHomeStatus(CORE, _cardNum, out homeStatus);
            } while (Convert.ToBoolean(homeStatus.run));
            mc.GTN_ZeroPos(CORE, _cardNum, 1);
            return stn;
        }
        public static short MonitorAsyGoHome(short CORE, short _cardNum, mc.THomePrm homePrm, mc.THomeStatus homeStatus)
        {
            mc.THomePrm prm;
            //打开使能
            mc.GTN_AxisOn(CORE, _cardNum);
            //设置回原点参数
            //  GTS.THomePrm prm = new GTS.THomePrm();
            //获取home参数
            mc.GTN_GetHomePrm(_cardNum, _cardNum, out prm);
            //回原点模式
            prm.mode = homePrm.mode;
            //搜索原点时的运动方向，1正向，0负方向
            prm.moveDir = homePrm.moveDir;
            //搜索index的运动方向，1正向，0负方向
            //prm.indexDir = 1;
            // 设置捕获沿：0-下降沿，1-上升沿 
            // prm.edge = 0;
            //用于设置触发器：取值-1 和[1,8]，i
            //-1 表示 使用的触发器和轴号对应，其它值表示使 用其它轴的触发器，
            //触发器用于实现高速 硬件捕获，默认设置为-1 即可 
            prm.triggerIndex = homePrm.triggerIndex;
            //回原点运动的搜索速度
            prm.velHigh = homePrm.velHigh;
            //回原点运动的定位速度
            prm.velLow = homePrm.velLow;
            //回原点运动的加速度
            prm.acc = homePrm.acc;
            // 回原点运动的减速
            prm.dec = homePrm.dec;
            // 回原点运动的平滑时间：取值[0,50]，单位：ms，
            //具体含义与 GTS 系列控制器点位运动相似
            prm.smoothTime = homePrm.smoothTime;
            // 设定的搜索 Home 的搜索范围，0 表示 搜索距离为 805306368 
            prm.searchHomeDistance = homePrm.searchHomeDistance;
            // 设定的搜索 Index 的搜索范围， 0 表示 搜索距离为 805306368 
            prm.searchIndexDistance = homePrm.searchIndexDistance;
            // 采用“限位回原点” 方式时，反方向离开 限位的脱离步长 
            // prm.escapeStep = 1000;
            //没有限位开关则取消限位,取消限位信息
            //GTS.GT_LmtsOn(_cardNum, i, 0);
            //清除轴状态
            mc.GTN_ClrSts(CORE, _cardNum, 1);
            //
            mc.GTN_ZeroPos(CORE, _cardNum, 1);
            //启动自动回原点
            short stn = mc.GTN_GoHome(CORE, _cardNum, ref prm);
            //do
            //{
            //    mc.GTN_GetHomeStatus(CORE, _cardNum, out homeStatus);
            //} while (Convert.ToBoolean(homeStatus.run));
            return stn;


        }
        private static int _cmpMode = 0;//wells0170

        /// <summary>
        /// 保存输出DO类型，【0，GPO；1，HSIO】
        /// </summary>
        private static List<int> _doType = new List<int>();//wells0185//改为列表，支持多通道数据
        //private static int _doType = 1;//wells0170

        /// <summary>
        /// 输出IO索引，默认是3
        /// </summary>
        private static List<int> _doChannel = new List<int>();//wells0185//改为列表，支持多通道数据
        //private static int _doChannel = 3;//wells0170

        /// <summary>
        /// gpo端口
        /// </summary>
        private static ushort _gpo = 0x0;//wells0185

        /// <summary>
        /// hso端口
        /// </summary>
        private static ushort _hso = 0x0;//wells0185
        public static bool setCompareMode(short[] channel, short[] doType, short cmpMode, short dimension, short sourceMode, short sourceX, short sourceY, short outputMode, ushort outputPulseWidth)//wells0173//wells0185//支持多通道比较
        {
            #region ***** 设置比较模式 *****
            //setCompareMode(new short[] { 3, 4 }, new short[] { 1, 1 }, 2, 1, 1, 5, 5, 0, 100)
            short nRetVal = 0;

            if (channel == null || doType == null || channel.Length != doType.Length)
                return false;

            nRetVal = mc.GTN_PosCompareStop(1, 1);//先关闭比较触发功能
            if (nRetVal != 0)
            {
                return false;
            }

            nRetVal = mc.GTN_PosCompareClear(1, 1);//清空比较数据缓存
            if (nRetVal != 0)
            {
                return false;
            }

            short permit = 0x2;

            _gpo = 0;
            _hso = 0;

            for (int igg = 0; igg < channel.Length; igg++)
            {
                short dataType = 0;
                ushort tmp = (ushort)(1 << (channel[igg] - 1));
                if (doType[igg] == 0)
                {
                    dataType = mc.MC_GPO;
                    _gpo |= tmp;
                }
                else
                {
                    dataType = mc.MC_HSO;
                    _hso |= tmp;
                }
                nRetVal = mc.GTN_SetTerminalPermitEx(1, 1, dataType, ref permit, channel[igg], 1);//设置比较通道
                if (nRetVal != 0)
                {
                    return false;
                }
            }

            mc.TPosCompareMode mode;
            nRetVal = mc.GTN_GetPosCompareMode(1, 1, out mode);//获取比较模式参数
            if (nRetVal != 0)
            {
                return false;
            }

            mode.mode = cmpMode;
            mode.dimension = dimension;
            mode.sourceMode = sourceMode;
            mode.sourceX = sourceX;
            mode.sourceY = sourceY;
            mode.outputMode = outputMode;
            mode.outputCounter = 1;
            mode.outputPulseWidth = outputPulseWidth;
            mode.errorBand = 0;

            nRetVal = mc.GTN_SetPosCompareMode(1, 1, ref mode);//设置比较模式参数
            if (nRetVal != 0)
            {
                return false;
            }

            _cmpMode = cmpMode;
            _doType.Clear();
            _doChannel.Clear();
            for (int igg = 0; igg < channel.Length; igg++)
            {
                _doType.Add(doType[igg]);
                _doChannel.Add(channel[igg]);
            }

            return true;

            #endregion
        }
        public static bool setCompareData_Pso(int interval)//wells0173
        {
            #region ***** 设置PSO数据 *****

            short nRetVal = 0;

            //nRetVal = mc.GTN_PosCompareClear(1, 1);//清空比较数据缓存
            //if (nRetVal != 0)
            //{
            //    return false;
            //}

            //mc.TPosCompareMode mode;
            //nRetVal = mc.GTN_GetPosCompareMode(1, 1, out mode);//获取比较模式参数
            //if (nRetVal != 0)
            //{
            //    return false;
            //}

            //if (mode.mode != 2 || _cmpMode != 2) //模式不匹配
            //    return false;

            mc.TPosComparePsoPrm pPrm;
            nRetVal = mc.GTN_GetPosComparePsoPrm(1, 1, out pPrm);
            if (nRetVal != 0)
            {
                return false;
            }

            pPrm.count = 1;
            pPrm.gpo = _gpo;
            pPrm.hso = _hso;
            pPrm.syncPos = interval;

            nRetVal = mc.GTN_SetPosComparePsoPrm(1, 1, ref pPrm);
            if (nRetVal != 0)
            {
                return false;
            }

            return true;

            #endregion
        }

        public static bool startCompare()//wells0173
        {
            #region ***** 开始比较功能 *****

            short nRetVal = 0;

            nRetVal = mc.GTN_PosCompareStart(1, 1);//开启比较触发功能
            if (nRetVal != 0)
            {
                return false;
            }

            return true;

            #endregion
        }

        public static bool stopCompare()//wells0173
        {
            #region ***** 停止比较功能 *****

            short nRetVal = 0;

            nRetVal = mc.GTN_PosCompareStop(1, 1);//先关闭比较触发功能
            if (nRetVal != 0)
            {
                return false;
            }

            return true;

            #endregion
        }
    }
}
